package cn.youframe.frame.security;

import cn.youframe.frame.base.BaseDaoImpl;
import cn.youframe.frame.beans.Menber;
import cn.youframe.frame.util.SqlBuilder;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

/**
 * User: Administrator
 * DateTime: 12-7-25: 下午4:19
 */
public class UserDaoImpl extends BaseDaoImpl implements UserDao {
    @Override
    public Menber findByName(String username) {
        Menber menber=null;
        try{
            if(username!=null && username.equalsIgnoreCase("admin")){
                menber=new Menber();
                menber.setAccount("admin");
                menber.setPassword("A]-Y[MWHL1");
                menber.setDid("0");
                menber.setEnable("0");
                menber.setPwdUpdateDays(0);
                menber.setExpiredDays(10);
            }else {
                SqlBuilder sbSql = new SqlBuilder();
                sbSql.append("select uid,account,password,username,did,utype,enable,(sysdate-PWD_UPDATE_DATE) PWD_UPDATE_DAYS,");
                sbSql.append("(EXPIRED_DATE-sysdate) EXPIRED_DAYS from yf_user where account=?");
                sbSql.addArg(username);
                menber=getJdbcTemplate().queryForObject(sbSql.getSQL(), BeanPropertyRowMapper.newInstance(Menber.class),sbSql.getArgArray());
            }
        }catch (Exception e){
            logger.error(e.getMessage(), e);
        }
        return menber;

    }

    @Override
    public int userOnline(String username) {
        int ret;
        try {
            ret=getJdbcTemplate().queryForObject("select count(ACCOUNT) from YF_ONLINE_USER where ACCOUNT=?",Integer.class,username);
        }catch (Exception ex){
            ret=-1;
            logger.info(ex.getMessage(),ex);
        }
        return ret;
    }
}
